package sketch.shape;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import sketch.doc.Sketch;
import sketch.shape.geom.prim.Point;

public class Shape implements Drawable {

	private int id;

	public int getId() {
		return id;
	}

	public String getName() {
		return this.getClass().getSimpleName();
	}

	public String getClassName() {
		return this.getClass().getName();
	}

	private List<Shape> children;

	private Sketch host;

	public List<Shape> getChildren() {
		return children;
	}

	public void setChildren(List<Shape> ch) {
		children = ch;
	}

	public boolean appendChild(Shape sh) {
		return children.add(sh);
	}

	public boolean appendChildren(Collection chn) {
		boolean suc = true;
		for (Iterator it = chn.iterator(); it.hasNext(); ) {
			Object ob = it.next();
			if (ob instanceof Shape) {
				if (!appendChild((Shape)ob))
					suc = false;
			}
		}
		return suc;
	}

	public boolean removeChild(Shape sh) {
		return children.remove(sh);
	}

	public boolean removeChildren(Collection col) {
		return children.removeAll(col);
	}

	public boolean removeChildrenAll() {
		return removeChildren(getChildren());
	}

	public int numOfChildren() {
		return children.size();
	}

	public Shape() {
		this.id = Sketch.generateId();
		children = new ArrayList<Shape>();
	}

	public void drawComponent(Graphics2D g2) {
	}

	public void draw(Graphics2D g2) {
		drawWith(g2, ShapeOption.drawColor, ShapeOption.drawWidth);
	}

	public void drawWith(Graphics2D g2, Color color, int width) {
		g2.setColor(color);
		g2.setStroke(new BasicStroke(width));

		drawComponent(g2);
	}

	public void drawSelected(Graphics2D g2) {
		g2.setColor(ShapeOption.selectionColor);
		g2.setStroke(ShapeOption.selectionStroke);
		g2.draw(getBound());
//		draw(g2, ShapeOption.selectionColor, ShapeOption.selectionWidth);
	}

	public Point getCenter() {
		return new Point();
	}

	public java.awt.Shape getBound() {
		return getBound(ShapeOption.selectionGap);
	}

	public java.awt.Shape getBound(double GAP) {
		return new Rectangle2D.Double(0, 0, 0, 0);
	}

	public String[][] getFieldValues() {
		return new String[][] {
				{ "", "" }
		};
	}
//	public boolean contains(Shape sh) {
//		return false;
//	}

	public void setHost(Sketch sketch) {
		host = sketch;
	}

	public Sketch getHost() {
//
//		if (host == null)
//			return SketchContainer.get().open();
		return host;
	}

	public boolean isBox() {
		return false;
	}

	public boolean isLink() {
		return false;
	}

	public boolean isFinal() {
		return false;
	}

}
